<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);
pc_base::load_sys_class('form', '', 0);
/**
 * 热搜词管理
 */
class hot extends admin {
	private $db;
	public $siteid;
	function __construct() {
		$this->db = pc_base::load_model('hot_search_model');
		$this->siteid = get_siteid();
		parent::__construct();
	}

    /**
     * 热搜词列表
     */
    public function init () {
        //获取管理员真实姓名数据
        $admin_model = pc_base::load_model('admin_model');
        $adminInfo = $admin_model->getRealnameInfo();

        $where = array();
        $where['status'] = 1; 
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        $list = $this->db->listinfo($where, 'listorder DESC,id DESC', $page, 10);
        $pages = $this->db->pages;
        pc_base::load_sys_class('format', '', 0);
        $big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=admin&c=hot&a=add\', title:\''.L('add_hot_search').'\', width:\'700\', height:\'150\'}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('add_hot_search'));
        include $this->admin_tpl('hot_list');
    }
	
    /**
     * 添加热搜词
     */
    public function add()
    {
        if (isset($_POST['dosubmit'])) {
            $_POST['hot']['create_time'] = date('Y-m-d H:i:s');
            $_POST['hot']['update_time'] = date('Y-m-d H:i:s');
            $_POST['hot']['created_by'] = $_SESSION['userid'];
            $_POST['hot']['updated_by'] = $_SESSION['userid'];
            $_POST['hot']['siteid'] = $this->siteid;

            $id = $this->db->insert($_POST['hot'],true);
            if($id) {

                //记录行为日志
                $log_content = '<span style="color:green;">添加热搜词</span>[新生成热搜词id：'.$id.'，热搜词：'.$_POST['hot']['content'].']';
                $this->manage_admin_log($log_content);

                showmessage(L('operation_success'),'?m=admin&c=hot&a=init','', 'add');
            } else {
                showmessage(L('operation_failure'), HTTP_REFERER);
            }
        } else {
            $show_validator = $show_scroll = $show_header = true;
            pc_base::load_sys_class('form', '', 0);
            include $this->admin_tpl('hot_add');
        }
    }

    /**
     * 热搜词修改
     */
    public function edit()
    {
        if (!isset($_GET['id']) || empty($_GET['id'])) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        $id = intval($_GET['id']);
        if (isset($_POST['dosubmit']) && !empty($_POST['dosubmit'])) {
            $_POST['hot']['update_time'] = date('Y-m-d H:i:s');
            $_POST['hot']['updated_by'] = $_SESSION['userid'];
            $ids = $this->db->update($_POST['hot'], array('id'=>$id));

            if ($ids) {

                //记录行为日志
                $log_content = '<span style="color:blue;">修改热搜词</span>[热搜词id：'.$id.'，热搜词：'.$_POST['hot']['content'].']';
                $this->manage_admin_log($log_content);

                showmessage(L('operation_success'), '?m=admin&c=hot&a=init','', 'edit');
            } else {
                showmessage(L('operation_failure'), HTTP_REFERER);
            }   
        } else {
            $show_validator = $show_scroll = $show_header = true;
            pc_base::load_sys_class('form', '', 0);
            $info = $this->db->get_one(array('id'=>$id, 'siteid'=>$this->siteid));
            extract($info);
            include $this->admin_tpl('hot_edit');
        }
    } 

    /**
     * 删除、批量删除
     */
    public function del()
    {
        if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        $id = $_REQUEST['id'];
        if (is_array($id)) {
            foreach ($id as $k => $v) {
                if (intval($v)) {
                    $id[$k] = intval($v);
                } else {
                    unset($id[$k]);
                }
            }
            $id = implode('\',\'', $id);
        }
        $result = $this->db->update(array('status'=>2), "id in ('$id')");

        //记录行为日志
        $id = safe_replace($id);
        $log_content = '<span style="color:red;">删除热搜词</span>[热搜词id：('.$id.')]';
        $this->manage_admin_log($log_content);

        if ($result) {
            showmessage(L('operation_success'), HTTP_REFERER);
        } else {
            showmessage(L('operation_failure'), HTTP_REFERER);
        }
    }

    /**
     * 排序
     */
    public function listorder() {
        if(isset($_GET['dosubmit'])) {
            foreach($_POST['listorders'] as $id => $listorder) {

                $result = $this->db->update(array('listorder'=>$listorder), array(
                    'id' => $id
                ));

            }
            showmessage(L('operation_success'), HTTP_REFERER);
        } else {
            showmessage(L('operation_failure'), HTTP_REFERER);
        }
    }
}
?>